//
//  AirplaneManager.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-25.
//
//

#import "AirplaneManager.h"
#import "ServerCommunicationManager.h"

#import "AirTicketSearchRequest.h"
#import "AirTicketSearchResponse.h"

#import "BuyAirTicketRequest.h"
#import "BuyAirTicketResponse.h"

@implementation AirplaneManager

static AirplaneManager* gManager = nil;

+ (AirplaneManager *)shareManager
{
    if (gManager == nil) {
        gManager = [[AirplaneManager alloc] init];
    }
    return gManager;
}

//查询机票
- (void)searchAirTicket:(NSString*)fromCity
                 toCity:(NSString*)toCity
                flyTime:(NSString*)flyTime
{
    AirTicketSearchRequest *request = [[AirTicketSearchRequest alloc] initWithFromCity:fromCity toCity:toCity flyTime:flyTime Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

//购买机票
- (void)buyAirTicket:(OrderAirInfo*)orderInfo
           userArray:(NSMutableArray*)userArray
           startData:(NSString*)startData
{
    BuyAirTicketRequest *request = [[BuyAirTicketRequest alloc] initBuyTicket:orderInfo
                                                                    userArray:userArray
                                                                    startData:startData
                                                                     Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}


- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response isKindOfClass:[AirTicketSearchResponse class]])
    {
        [self handleQryPlainSuccess:(AirTicketSearchResponse*)response];
    }
    else if ([response isKindOfClass:[BuyAirTicketResponse class]])
    {
        [self handleBuyAirTicketSuccess:(BuyAirTicketResponse*)response];
    }
}



- (void)onRequestFail:(BaseResponse *)response
{
    if ([response isKindOfClass:[AirTicketSearchResponse class]])
    {
        [self.delegate qryPlainInfo:NO AirInfo:nil];
    }
    else if ([response isKindOfClass:[BuyAirTicketResponse class]])
    {
        [self.delegate buyAirTicket:NO tn:nil];
    }
}


//查询机票
- (void)handleQryPlainSuccess:(AirTicketSearchResponse*)response
{
    AirPlaceInfo *airInfo = response.airInfo;
    if (airInfo)
    {
        [self.delegate qryPlainInfo:YES AirInfo:airInfo];
    }
    else
    {
        [self.delegate qryPlainInfo:NO AirInfo:nil];
    }
}

//购买机票
- (void)handleBuyAirTicketSuccess:(BuyAirTicketResponse*)response
{
    NSString *tn = response.tn;
    if (tn)
    {
        [self.delegate buyAirTicket:YES tn:tn];
    }
    else
    {
        [self.delegate buyAirTicket:NO tn:nil];
    }
}

@end
